package com.project.controller.web;

import com.alibaba.druid.util.StringUtils;
import com.project.common.utils.MyPageHelper;
import com.project.common.utils.PublicUtils;
import com.project.common.validator.ValidatorUtils;
import com.project.common.validator.group.AddGroup;
import com.project.entity.*;
import com.project.myBeans.R;
import com.project.myBeans.TeacherInfoDetail;
import com.project.service.*;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.models.auth.In;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.autoconfigure.EnableAutoConfiguration;
import org.springframework.web.bind.annotation.*;
import com.project.dao.PaperShareMapper;
import com.project.service.UserService;

import java.util.ArrayList;
import java.util.LinkedList;
import java.util.List;

@EnableAutoConfiguration
@RestController
@Api(description = "试卷表接口", value = "ExamPaper")
@RequestMapping("/exampaper")
public class ExamPaperController {

    @Autowired
    ExamPaperService examPaperService;

    @Autowired
    PaperShareService paperShareService;

    @Autowired
    CourseService courseService;

    @Autowired
    CourseTeachService courseTeachService;

    @Autowired
    UserService userService;

    /**
     * Tate 2018/3/27
     *
     * @param examPaper
     * @return
     */
    @PostMapping("/add")
    @ApiOperation("添加试卷")
    public R addExamPaper(@RequestBody ExamPaper examPaper) {
        try {
            if (examPaperService.add(examPaper) > 0) {
                return R.ok("试卷记录生成成功!");
            } else {
                return R.error("试卷记录生成失败!");
            }
        } catch (Exception e) {
            e.printStackTrace();
            return R.error();
        }
    }

    /**
     * Tate 2018/3/27
     *
     * @param id
     * @return
     */
    @ApiOperation("通过id删除试卷")
    @GetMapping("/remove/{id}")
    public R removeOne(@PathVariable Integer id) {
        examPaperService.remove(id);
        return R.ok();
    }

    /**
     * Tate 2018/3/27
     *
     * @param examPaper
     * @return
     */
    @ApiOperation("分页")
    @PostMapping("/findByCondition")
    public R findByCondition(@RequestBody ExamPaper examPaper) {
        try {
            List<ExamPaper> list = examPaperService.findByCondition(examPaper);
            return R.ok().put("data", list).put("total", MyPageHelper.getTotal(list));
        } catch (Exception e) {
            e.printStackTrace();
            return R.error();
        }
    }

    /**
     * Tate 2018/4/11
     *
     * @param examPaper
     * @return
     */
    @ApiOperation("通过教师ID查找试卷")
    @PostMapping("/findByTeacherid/{teacherid}")
    public R findByTeacherid(@PathVariable Integer teacherid, @RequestBody ExamPaper examPaper) {
        try {
            MyPageHelper.setPageParam(examPaper);
            List<ExamPaper> list = examPaperService.findByTeacherid(teacherid);
            return R.ok().put("data", list).put("total", MyPageHelper.getTotal(list));
        } catch (Exception e) {
            e.printStackTrace();
            return R.error();
        }
    }

    /**
     * Tate 2018/4/12
     *
     * @param teacherid,examPaper
     * @return
     */
    @ApiOperation("更新试卷信息")
    @PostMapping("/change/{teacherid}")
    public R modify(@PathVariable Integer teacherid, @RequestBody ExamPaper examPaper) {
        if (examPaper.getTeacherid() == teacherid) {
            ValidatorUtils.validateEntity(examPaper, AddGroup.class);
            examPaperService.modify(examPaper);
            return R.ok();
        } else {
            return R.error("你无修改此试卷的权限!");
        }
    }

    /**
     * Tate 2018/4/12
     *
     * @param shareteacherids
     * @param examPaperid
     * @return
     */
    @ApiOperation("试卷共享操作")
    @PostMapping("/add/{shareteacherids}/{examPaperid}")
    public R shareexamPaper(@PathVariable String shareteacherids, @PathVariable Integer examPaperid) {
        try {
            List<Integer> shareteacheridlist = new LinkedList<>();
            if (!StringUtils.isEmpty(shareteacherids) && !shareteacherids.equals("N")) {
                shareteacheridlist = PublicUtils.getIntIdList(shareteacherids, ",");
            }
            paperShareService.addBatch(shareteacheridlist, examPaperid);
            return R.ok("共享成功!");
        } catch (Exception e) {
            e.printStackTrace();
            return R.error();
        }
    }

    /**
     * Tate 2018/4/15
     *
     * @param teacherid,courseid
     * @return
     */
    @ApiOperation("根据课程id与教师id试卷查找操作")
    @PostMapping("/findBycondition/{teacherid}/{courseid}")
    public R findExamPaperbycourseandteacher(@PathVariable Integer teacherid, @PathVariable Integer courseid) {
        try {
            List<ExamPaper> list = examPaperService.findByCourseid(teacherid, courseid);
            List<PaperShare> listt = paperShareService.findByTeacherid(teacherid);
            for (PaperShare papershare : listt) {
                if (examPaperService.findById(papershare.getExampaperid()) != null)
                    if (examPaperService.findById(papershare.getExampaperid()).getCourseid() == courseid)
                        list.add(examPaperService.findById(papershare.getExampaperid()));
            }
            return R.ok().put("data", list).put("total", MyPageHelper.getTotal(list));
        } catch (Exception e) {
            e.printStackTrace();
            return R.error();
        }
    }

    /**
     * Tate 2018/4/15
     *
     * @param id
     * @return
     */
    @ApiOperation("根据试卷id查找试卷")
    @PostMapping("/findByid/{id}")
    public R findExamPaperbyid(@PathVariable Integer id) {
        try {
            ExamPaper examPaper = examPaperService.findById(id);
            return R.ok().put("data", examPaper);
        } catch (Exception e) {
            e.printStackTrace();
            return R.error();
        }
    }

//    /**
//     * Tate 2018/5/16
//     *
//     * @param courseid
//     * @return
//     */
//    @ApiOperation("根据课程id返回当前课程教师的姓名")
//    @PostMapping("/findNameBycourseid/{courseid}")
//    public R findNameBycourseid(@PathVariable Integer courseid) {
//        try {
////            Course course = courseService.findById(courseid);
//            List<TeacherInfoDetail> teacherInfoDetails = courseTeachService.findTeachersByCourseid(courseid);
//           return R.ok().put("data",teacherInfoDetails).put("total",MyPageHelper.getTotal(teacherInfoDetails));
//        } catch (Exception e) {
//            e.printStackTrace();
//            return R.error();
//        }
//    }
    /**
     * Tate 2018/4/15
     *
     * @param exampaperid
     * @return
     */
    @ApiOperation("根据试卷id找到被共享的所有教师")
    @PostMapping("/findByexampaperid/{exampaperid}")
    public R findByexampaperid(@PathVariable Integer exampaperid) {
        try {
            List<Integer> list = new LinkedList<>();
            List<PaperShare> listt = paperShareService.findByexamPaperid(exampaperid);
            for (PaperShare paperShare : listt) {
                list.add(paperShare.getTeacherid());
            }
            return R.ok().put("data", list);
        } catch (Exception e) {
            e.printStackTrace();
            return R.error();
        }
    }
}



